package com.softeem.ssm2.controller;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageInterceptor;
import com.softeem.ssm2.entity.Student;
import com.softeem.ssm2.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Author mrchai
 * @Date 2019/10/12 10:38
 */
@RequestMapping("/stu")
@Controller
public class StudentController {

    @Autowired
    private IStudentService studentService;

    @ResponseBody
    @RequestMapping("/list")
    public List<Student> listStu(){
        return studentService.queryAll();
    }

    @ResponseBody
    @RequestMapping("/query/id/{id}")
    public Student queryById(@PathVariable Integer id){
        return studentService.queryById(id);
    }

    @RequestMapping("/add")
    public ModelAndView addStu(@ModelAttribute Student stu){
        boolean b = studentService.add(stu);
        ModelAndView mv = new ModelAndView();
        if(b){
            mv.setViewName("list");
        }
        mv.setViewName("add");
        return mv;
    }

    @ResponseBody
    @RequestMapping("/page")
    public PageInfo<Student> page(Integer pagenow,Integer pagesize){
        return studentService.queryByPage(pagenow,pagesize);
    }
}
